#include "Recipe.h"

//using juce;

Recipe::Recipe()
{

}

Recipe::Recipe(const String &xmlpath)
{
	ingredients = String::empty;
	directions = String::empty;

	File xmlfile = File(xmlpath);
	if(!xmlfile.existsAsFile()) {
		throw("XML file not found");
	}

	/* If this fails one of us provided some seriously broken XML, or Juce can't
	 * parse XML according to version 1.1. */
	topelem = XmlDocument(xmlfile).getDocumentElement();
	if(!topelem) {
		throw("XML file corrupt");
	}

	/* Run through the document and fill in variables */
	XmlElement* ingredientsList = topelem->getChildByName(T("ingredients"));
	XmlElement* directionsList = topelem->getChildByName(T("directions"));

	/* Prep/cooking time and servings */
	cookTime = topelem->getIntAttribute(T("cook"), 0);
	prepTime = topelem->getIntAttribute(T("prep"), 0);
	servings = topelem->getIntAttribute(T("servings"), 0);
	title = topelem->getStringAttribute(T("name"), String("Unknown Recipe"));

	/* Now the fun stuff. Parse out the ingredients. */
	int nChildren = ingredientsList->getNumChildElements();
	int i = 0;
	for(i = 0; i < nChildren; i++) {
		XmlElement* child = ingredientsList->getChildElement(i);
		if(child->getTagName() != T("ingredient")) {
			continue;
		}

		foodID.push_back(child->getIntAttribute(T("id"), -1));

		XmlElement* quantity = child->getChildByName(T("quantity"))->getFirstChildElement();
		XmlElement* measurement = child->getChildByName(T("measurement"))->getFirstChildElement();
		XmlElement* name = child->getChildByName(T("name"))->getFirstChildElement();

		if(0 != quantity) {
			ingredients += quantity->getText().trim();
			ingredients += T(" ");
		}

		if(0 != measurement) {
			ingredients += measurement->getText().trim();
			ingredients += T(" ");
		}

		ingredients += name->getText().trim();
		ingredients += T("\n");
	}

	nChildren = directionsList->getNumChildElements();
	int count = 0;
	for(i = 0; i < nChildren; i++) {
		XmlElement* child = directionsList->getChildElement(i);
		if(child->getTagName() != T("direction")) {
			continue;
		}

		directions += String(++count);
		directions += ". ";
		directions += child->getAllSubText().trim();
		directions += T("\n");
	}
}

Recipe::~Recipe()
{
//	delete (topelem);
	//	delete (title);
}

int Recipe::getCookTime() const
{
	return cookTime;
}

int Recipe::getPrepTime() const
{
	return prepTime;
}

int Recipe::getServings() const
{
	return servings;
}

String Recipe::getTitle() const
{
	return title;
}

TextLayout Recipe::getIngredients() const
{
	return TextLayout(ingredients, Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));
}

TextLayout Recipe::getDirections() const
{
	return TextLayout(directions, Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));
}

TextLayout Recipe::getTimes() const
{
	int srv = getServings();
	int prep = getPrepTime();
	int cook = getCookTime();
	String s = String("Servings: ");
	s += String(srv);
	s += "\n";

	s += "Preparation Time: ";
	s += String(prep);
	s += "min\n";

	s += "Cooking Time: ";
	s += String(cook);
	s += "min\n";

	s += "Total Time: ";
	s += String((prep + cook));
	s += "min";

	return TextLayout(s, Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));
}

std::vector<int> Recipe::getFoodID() const
{
	std::vector<int> v;

	for(int i = 0; i < (int) foodID.size(); i++) {
		v.push_back(foodID[i]);
	}

	return v;
}

void Recipe::operator =(const Recipe& other)
{
	this->cookTime = other.getCookTime();
	this->directions = other.getDirectionsString();
	this->foodID = other.getFoodID();
	this->ingredients = other.getIngredientsString();
	this->prepTime = other.getPrepTime();
	this->servings = other.getServings();
	this->title = other.getTitle();
}

String Recipe::getIngredientsString() const {
	return ingredients;
}

String Recipe::getDirectionsString() const {
	return directions;
}
